package chuishop.yiqimaicai.controller;


import chuishop.yiqimaicai.pojo.Typegoods;
import chuishop.yiqimaicai.service.TypegoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TypegoodsController {

    @Autowired
    private TypegoodsService typegoodsService;

    @RequestMapping("findAllTypegoods")
    @ResponseBody
    public List<Typegoods> findAllTypegoods(){
        List<Typegoods> list=typegoodsService.findAllTypegoods();
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getTgname());
        }
        return list;
    }


    @RequestMapping("addTypegoods")
    @ResponseBody
    public Map<Object, Object> addTypegoods(@RequestBody Typegoods typegoods) {
        System.out.println(typegoods.getTgid() + typegoods.getTgname());

        Map<Object, Object> result = new HashMap<>();
        boolean flag =typegoodsService.addTypegoods(typegoods);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("delTypegoods")
    @ResponseBody
    public Map<Object, Object> delTypegoods(String ids) {
        System.out.println(ids);

        String idstr[] = ids.split(",");
        for (String str : idstr) {
            System.out.println(str);
        }
        System.out.println(idstr.length);

        Map<Object, Object> result = new HashMap<>();
        boolean flag = typegoodsService.delTypegoods(idstr);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }
}
